<?php
/**
 * User: hjd-yao
 * Date: 2018/11/5
 * Time: 17:15
 */

namespace App\Http\Middleware\Backend;


use App\Helpers\SelfRoutes;
use App\Models\ManageAdminModel;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class CheckIsLogin extends Middleware
{
    public function handle(Request $request, Closure $next)
    {
        if(!ManageAdminModel::init()->isLogin()){
            return $this->responseMethod([
                'url'=>route('b_Login_showLoginForm'),
            ]);
        }
        if(!$this->notAllowedLoginInMultiClient()){
            ManageAdminModel::init()->logout();
            return $this->responseMethod([
                'url'=>route('b_Login_showLoginForm'),
                'msg'=>'该账户已在其它设备上登录'
            ]);
        }
        $this->loginData();
        return $next($request);
    }

    private function loginData()
    {
        $login_menu = ManageAdminModel::init()->getLoginMenu();
        $my_actions = ManageAdminModel::init()->myActions();

        view()->share('login_info',ManageAdminModel::init()->getLoginInfo());
        view()->share('login_menu',$login_menu);
        view()->share('all_actions',$my_actions);

        if(strpos(Route::currentRouteAction(),'Develop') !== false){
            if(!request()->route()->parameters()){
                $current_route = Route::currentRouteName();
                session(['current_route'=>$current_route]);
            }else{
                $current_route = \request()->session()->get('current_route');
            }
        }else{
            if($this->getFocusName($login_menu)){
                $current_route = Route::currentRouteName();
                session(['current_route'=>$current_route]);
            }else{
                $current_route = \request()->session()->get('current_route',Route::currentRouteName());
            }
        }
        view()->share('current_route',$current_route);
        $this->getPageTitle(Route::currentRouteName());

    }

    private function getPageTitle($current_route)
    {
        $info = SelfRoutes::make()->setRoutes()->getAllInfo();
        $title = config('website.website_title');
        if($info&&isset($info[$current_route]['title'])){
            $title = $info[$current_route]['title'];
        }
        view()->share('page_title',$title);
    }

    private function getFocusName($menu)
    {
        $current_name = Route::currentRouteName();
        foreach ($menu as $column){
            if($column['url'] == $current_name){
                return true;
            }elseif (isset($column['_child'])){
                if($this->getFocusName($column['_child'])){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @title 不允许在多个客户端中登录
     */
    public function notAllowedLoginInMultiClient()
    {
        return true;
        $login_info = ManageAdminModel::init()->getLoginInfo();
        $my_name = $login_info['mgr_name'];
        $my_token = $login_info['mgr_token'];
        $token = cache()->get('backend_'.$my_name);
        if($token&&$my_token!=$token){
            return false;
        }
        return true;
    }
}